# Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
# License: GNU General Public License v3. See license.txt


from collections import defaultdict
from json import loads
from typing import TYPE_CHECKING, Optional

import frappe
import frappe.defaults
from frappe import _, qb, throw
from frappe.desk.reportview import build_match_conditions
from frappe.model.meta import get_field_precision
from frappe.query_builder import AliasedQuery, Case,Criterion, Table
from frappe.query_builder.functions import Count,Max,Sum
from frappe.query_builder.utils import DocType
from frappe.utils import(
    add_days,
    cint,
    create_batch,
    cstr,
    flt,
    formatdate,
    get_datetime,
    get_number_format_info,
    getdate,
    now,
    nowdate
)
from pypika import Order
from pypika.functions import Coalesce
from pypika.terms import ExistsCriterion

import kingdee

# imported to enable erpnext.accounts.util.get_account_currency
from kingdee.accounts.doctype.account.account import get_account_currency
from kingdee.accounts.doctype.accounting_dimension.accounting_dimension import get_dimnsion
from kingdee.stock import get_warehouse_account_map
from kingdee.stock.utils import get_stock_value_on